home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 551-575 / disk_556 / scheme2c / scheme-doc.lzh / doc / intro.mss < prev    next >
Text File  |  1991-10-11  |  9KB  |  366 lines

  1. @Style(FontFamily <TimesRoman>, Singlesided)
  2.  
  3. @Begin(Heading)
  4. An Introduction to Scheme->C in 19 Prompts
  5. 12 August 1988
  6. @End(Heading)
  7.  
  8. @t(Scheme->C) is an implementation of the language Scheme.  Besides
  9. the usual interpreter, the implementation includes an unusual
  10. compiler which compiles Scheme to C.  This allows stand-alone
  11. programs and programs combining Scheme and other programming
  12. languages.  The implementation is also highly portable and when
  13. combined with a good C compiler, fairly efficient.
  14.  
  15. Please consider this annotated typescript an invitation to try it.
  16. Questions and comments are encouraged.
  17.  
  18. @Begin(Verbatim)
  19. alerion 1 >man sci
  20.  
  21. SCI(1)               UNIX Programmer's Manual                SCI(1)
  22.  
  23. NAME
  24.      sci - Scheme interpreter
  25.  
  26. SYNTAX
  27.      sci [ option ]
  28.  
  29. DESCRIPTION
  30.      The sci command invokes a Scheme interpreter.  The language
  31.      accepted by ...
  32. @End(Verbatim)
  33.  
  34. Using your favorite editor, create a file containing the Fibonacci
  35. function.  Scheme code is generally expected to be in files ending
  36. with ".sc".
  37.  
  38. @Begin(Verbatim)
  39. alerion 2>more fib.sc
  40. ;;; (FIB n) returns the Fibonacci number for n.
  41.  
  42. (module fib)
  43.  
  44. (define (FIB n)
  45.     (cond ((> n 1) (+ (fib (- n 1)) (fib (- n 2))))
  46.       ((= n 1) 1)
  47.       ((= n 0) 0)
  48.       (else  (error 'FIB "Argument is out of range: ~s" n))))
  49. @End(Verbatim)
  50.  
  51. A ";" indicates that the rest of the line is a comment.  The form
  52. @t[(module fib)] which must be the first form is the file indicates
  53. that the functions in the file should be part of the module "fib".
  54. Typically the module name is the file name (less the ".sc" extension)
  55. of the source file.
  56.  
  57. @Begin(Verbatim)
  58. alerion 3 >sci
  59. SCHEME->C -- 08aug88jfb -- Copyright 1988 Digital Equipment Corporation
  60. > (load "fib.sc")
  61. MODULE form ignored
  62. FIB
  63. "fib.sc"
  64. @End(Verbatim)
  65.  
  66. (@t[load] "@i(file-name)") loads a file into the interpreter.  Each
  67. form in the file is evaluated and the result is printed on the
  68. standard output device.  Note that the module form is currently
  69. ignored by the interpreter.  Another way to load the file is to use
  70. (@t[loade] "@i(file-name)") which will also echo the text read from
  71. the file onto the standard output file.  Since @t(FIB) was defined
  72. when this function was evaluated, a warning message is printed:
  73.  
  74. @Begin(Verbatim)
  75. > (loade "fib.sc")
  76. ;;; (FIB n) returns the Fibonacci number for n.
  77.  
  78. (module fib)
  79.  
  80. MODULE form ignored
  81. (define (FIB n)
  82.     (cond ((> n 1) (+ (fib (- n 1)) (fib (- n 2))))
  83.       ((= n 1) 1)
  84.       ((= n 0) 0)
  85.       (else  (error 'FIB "Argument is out of range: ~s" n))))
  86. ***** FIB is redefined
  87.  
  88. FIB
  89. "fib.sc"
  90. > (fib 1)
  91. 1
  92. > (fib 2)
  93. 1
  94. > (fib 0)
  95. 0
  96. > (fib -1)
  97. ***** FIB Argument is out of range: -1
  98. @End(Verbatim)
  99.  
  100. (@t[trace] @i(function) ...) allows one or more functions to be
  101. traced. (@t[untrace] @i(function) ...) removes tracing from selected
  102. functions, and (@t[untrace]) removes tracing from all functions.
  103.  
  104. @Begin(Verbatim)
  105. > (trace fib)
  106. (FIB)
  107. > (fib 5)
  108. (FIB 5)
  109.   (FIB 4)
  110.     (FIB 3)
  111.       (FIB 2)
  112.         (FIB 1)
  113.         ==> 1
  114.         (FIB 0)
  115.         ==> 0
  116.       ==> 1
  117.       (FIB 1)
  118.       ==> 1
  119.     ==> 2
  120.     (FIB 2)
  121.       (FIB 1)
  122.       ==> 1
  123.       (FIB 0)
  124.       ==> 0
  125.     ==> 1
  126.   ==> 3
  127.   (FIB 3)
  128.     (FIB 2)
  129.       (FIB 1)
  130.       ==> 1
  131.       (FIB 0)
  132.       ==> 0
  133.     ==> 1
  134.     (FIB 1)
  135.     ==> 1
  136.   ==> 2
  137. ==> 5
  138. 5
  139. > (untrace)
  140. (FIB)
  141. @End(verbatim)
  142.  
  143. (@t[bpt] @i[function]) sets a breakpoint on function entry and
  144. exit. At the function call, the arguments are in @t[*args*]
  145. which may be changed. After completing inspection, type ^D to
  146. evaluate the function.  On function exit, the result is in
  147. @t[*result*] and the program stops for inspection.  To
  148. continue with that result, one types ^D or (@t[proceed]).  A
  149. different result may be returned by entering (@t[proceed]
  150. @i(expression)).  (@t[unbpt] @i(function) ...) removes
  151. breakpoints from selected functions, and (@t[unbpt]) removes
  152. all breakpoints. While at a breakpoint, one may return to the
  153. "top-level" interpreter by executing the function
  154. (@t[top-level]).
  155.  
  156. @Begin(Verbatim)
  157. > (bpt fib)
  158. FIB
  159. > (fib 1)
  160.  
  161. 0 -calls  - (FIB 1)
  162. 0- *args*
  163. (1)
  164. 0- ^D
  165. 0 -returns- 1
  166. 0- *result*
  167. 1
  168. 0- (proceed 23.7)
  169. 23.7
  170. 0- ^D
  171. 23.7
  172. > (unbpt fib)
  173. (FIB)
  174. @End(Verbatim)
  175.  
  176. Breakpoints may also have a boolean function supplied which decides
  177. whether to take the breakpoint.  Needless to say, such a function can
  178. also do things like count the number of times the function is called.
  179.  
  180. @Begin(Verbatim)
  181. > (bpt fib (lambda (n) (set! fibcnt (+ 1 fibcnt)) #f))
  182. FIB
  183. > (set! fibcnt 0)
  184. 0
  185. > (fib 5)
  186. 5
  187. > fibcnt
  188. 15
  189. > (unbpt fib)
  190. (FIB)
  191. > (fib 20)
  192. 6765
  193. >^D
  194. alerion 4 >
  195. @End(Verbatim)
  196.  
  197. Since @t[(fib 20)] took a while to compute, it might be a good idea
  198. to compile it, so the interpreter is exited, and an augmented version
  199. of the interpreter is created which has a compiled version of FIB.
  200.  
  201. @Begin(Verbatim)
  202. alerion 4 >man scc
  203.  
  204. SCC(1)              UNIX Programmer's Manual               SCC(1)
  205.  
  206. NAME
  207.      scc - Scheme to C compiler
  208.  
  209. SYNTAX
  210.      scc [ option ] ... file ...
  211.  
  212. DESCRIPTION
  213.      The scc command invokes a Scheme compiler which accepts the
  214.      language ...
  215.  
  216. alerion 5 >scc -i -o sc+fib fib.sc
  217. fib.sc:
  218. fib.c:
  219. SC-TO-C.c:
  220. alerion 6 >sc+fib
  221. SCHEME->C -- 08aug88jfb -- Copyright 1988 Digital Equipment Corporation
  222. > fib
  223. #*PROCEDURE*
  224. > (fib 1)
  225. 1
  226. > (fib 0)
  227. 0
  228. > (fib 20)
  229. 6765
  230. >^D
  231. @End(Verbatim)
  232.  
  233. Now for a little different example, where a Scheme version of the
  234. shell command "echo" is created as a stand alone program. The module
  235. form now has an additional component, @t[(main do-echo)], which
  236. indicates that the @t[do-echo] function is the program main. As with
  237. any other UNIX program, the main is called with the arguments from
  238. the shell.  This is done in Scheme by providing the main with a list
  239. of strings which are the arguments.
  240.  
  241. @Begin(Verbatim)
  242. alerion 7 >more echo.sc
  243. ;;; ECHO - Echo Arguments
  244. ;;;
  245. ;;; % echo [options] [args]
  246. ;;;
  247. ;;; Option:
  248. ;;;    -n    newlines are not added to output
  249.  
  250. (module echo (main do-echo))
  251.  
  252. (define (DO-ECHO clargs)
  253.     (let ((nonewline (and (cdr clargs) (equal? (cadr clargs) "-n"))))
  254.      (do ((args (if nonewline (cddr clargs) (cdr clargs)) (cdr args)))
  255.          ((null? args)
  256.           (unless nonewline (newline)))
  257.          (display (car args))
  258.          (if (cdr args) (display " ")))))
  259. alerion 8>
  260. @End(Verbatim)
  261.  
  262. The program is loaded into the interpreter and tested with a few
  263. possible values.  Note that the first argument is always the program
  264. name.
  265.  
  266. @Begin(Verbatim)
  267. alerion 8>sci
  268. SCHEME->C -- 08aug88jfb -- Copyright 1988 Digital Equipment Corporation
  269. > (load "echo.sc")
  270. MODULE form ignored
  271. DO-ECHO
  272. "echo.sc"
  273. > (do-echo '("echo" "-n" "a"))
  274. a#F
  275. > (do-echo '("echo"  "a" "b" "c"))
  276. a b c
  277. #F
  278. > (do-echo '("echo" "-n" "a" "b" "c"))
  279. a b c#F
  280. > (do-echo '("echo"))
  281.  
  282. #F
  283. > ^D
  284. @End(Verbatim)
  285.  
  286. Now, compile it and build a stand-alone program:
  287.  
  288. @Begin(Verbatim)
  289. alerion 9 >scc -o scheme-echo echo.sc
  290. echo.sc:
  291. alerion 10 >scheme-echo *.sc
  292. counter.sc echo.sc fib.sc fsm2.sc fsmexample.sc hello.sc
  293. alerion 11 >scheme-echo -n *.sc
  294. counter.sc echo.sc fib.sc fsm2.sc fsmexample.sc hello.scalerion 12 >
  295. @End(Verbatim)
  296.  
  297. The next example shows the interface to routines written in other
  298. languages by building a program which uses the routines in the C library 
  299. (described in the @i(ULTRX-32 Programmer's Manual)) to print out the
  300. current Greenwich mean time.
  301.  
  302. @Begin(Verbatim)
  303. alerion 12 >more gmt.sc
  304. ;;; Print current GMT on standard output.
  305.  
  306. (module gmt (main gmt))
  307.  
  308. (define-c-external (time pointer) int "time")
  309. (define-c-external (gmtime pointer) pointer "gmtime")
  310. (define-c-external (asctime pointer) pointer "asctime")
  311.  
  312. (define (GMT clargs)
  313.     (let ((current-time (make-string 4)))
  314.      (time current-time)
  315.      (display (c-string->string (asctime (gmtime current-time))))))
  316. @End(Verbatim)
  317.  
  318. The procedure @i(time) stores the number of seconds since GMT. Jan.
  319. 1. 1970 in the location referenced by @i(pointer). @i(gmtime)
  320. converts that value to a @i(tm) structure and returns a pointer to
  321. it. @i(asctime) then converts the referenced @i(tm) structure to a
  322. string and returns a pointer to it.  In order to display it,
  323. @i(c-string->string) is used to make a Scheme copy of the string.
  324.  
  325. @Begin(Verbatim)
  326. alerion 13 >scc -o gmt gmt.sc
  327. gmt.sc:
  328. alerion 14 >gmt
  329. Thu Aug 11 22:19:08 1988
  330. @End(Verbatim)
  331.  
  332. To allay any doubts that this implementation might not be
  333. Scheme, we conclude with the following "proof by example",
  334. produced by Eugene Kohlbecker:
  335.  
  336. @Begin(Verbatim)
  337. alerion 15 >more mondo.sc
  338. (module mondo (main call-mondo))
  339.  
  340. (define (call-mondo clargs) (mondo-bizarro) (newline))
  341.  
  342. (define (mondo-bizarro)
  343.     (let ((k (call-with-current-continuation (lambda (c) c))))
  344.      (display 1)
  345.      (call-with-current-continuation (lambda (c) (k c)))
  346.      (display 2)
  347.      (call-with-current-continuation (lambda (c) (k c)))
  348.      (display 3)))
  349. alerion 16 >sci
  350. SCHEME-C -- 08aug88jfb -- Copyright 1988 Digital Equipment Corporation
  351. > (load "mondo.sc")
  352. MODULE form ignored
  353. CALL-MONDO
  354. MONDO-BIZARRO
  355. "mondo.sc"
  356. > (call-mondo '())
  357. 11213
  358. #F
  359. > ^D
  360. alerion 17 >scc -o mondo mondo.sc
  361. mondo.sc:
  362. alerion 18 >mondo
  363. 11213
  364. alerion 19 >logout
  365. @End(Verbatim)
  366.